<?php

namespace App\Services\Feedback;


use App\Exceptions\ApiExceptions;
use App\Helpers\FunTool;
use App\Models\FeedbackModel;
use App\Models\JobpositionModel;
use App\Services\AbstractBaseService;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

class FeedbackService extends AbstractBaseService
{
    /**
     * 问题反馈类型
     * @var array
     */
    const FEEDBACK_SCENE = [
        1 => '虚假招聘/违法信息',
        2 => '违法收费',
        3 => '骗取钱财',
        4 => '黑中介',
        5 => '公司信息作假',
        6 => '求职作假',
        7 => '违规行为',
        8 => '其他',
    ];

    public static function init()
    {
        $feedback_scene = array_values(self::FEEDBACK_SCENE);
        $max_img = 6;
        return compact('feedback_scene', 'max_img');
    }

    /**
     * 提交反馈
     * @param int $userId
     * @param int $scene
     * @param string $content
     * @param int $job_position_id
     * @param array $feed_pic
     * @return void
     * @throws ApiExceptions
     */
    public static function create(int $userId, int $scene, string $content, int $job_position_id, array $feed_pic = [])
    {
        if (!in_array($scene,array_keys(self::FEEDBACK_SCENE))){
            throw new ApiExceptions('类型选择错误');
        }
        if (JobpositionModel::query()->where('id', $job_position_id)->doesntExist()){
            throw new ApiExceptions('职位信息错误');
        }
        $res = FeedbackModel::query()->create([
            'job_position_id' => $job_position_id,
            'user_id' => $userId,
            'scene' => $scene,
            'content' => $content,
            'feed_pic' => $feed_pic,
        ]);
        if (!$res){
            throw new ApiExceptions('操作失败，请稍后重试!');
        }

    }

    /**
     * 反馈列表
     * @param int $userId
     * @return array|LengthAwarePaginator
     */
    public static function lists(int $userId)
    {
        $list = FeedbackModel::query()->where('uid', $userId)
            ->select(['title', 'content', 'feed_pic', 'cur_status', 'scene', 'response', 'created_at', 'updated_at'])
            ->paginate();
        foreach ($list as $value) {
            $value->scene = self::FEEDBACK_SCENE[$value->scene];
            if (strlen($value->feed_pic) > 10) {
                $feed_pic = explode('|', $value->feed_pic);
                foreach ($feed_pic as $key => $item) {
                    $feed_pic[$key] = FunTool::imageUrl($item);
                }
                $value->feed_pic = $feed_pic;
            } else {
                $value->feed_pic = [];
            }
        }
        return $list;

    }

}
